package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.sayHello();

        /*//实例化
        Class cls = Class.forName("reflect.Person");
        Object o = cls.newInstance();//new Person
        //调用方法sayHello()
        Method method = cls.getMethod("sayHello");
        //p.sayHello();
        method.invoke(o);//o.sayHello()*/
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名：");
        String className = scanner.nextLine();
        System.out.println("请输入方法名：");
        String methodName = scanner.nextLine();

        Class cls = Class.forName(className);
        Object o = cls.newInstance();//new Person
        //调用方法sayHello()
        Method method = cls.getMethod(methodName);
        //p.sayHello();
        method.invoke(o);//o.sayHello()
    }
}
